//
//  WmcLocatBtn.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/25.
//

import UIKit

class WmcHomeLocalBtn: WmcTopImgBottomTextBtn {
    lazy var animationImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.masksToBounds = true
        imageView.layer.cornerRadius = 4
        imageView.contentMode = .scaleToFill
        return imageView
    }()

    static func create() -> WmcHomeLocalBtn {
        let btn = WmcHomeLocalBtn(title: "本地相册", image: "home_tool_preview".wmc_image)
        btn.yp.layoutWidthEqual(50.wmc_autoHor)
        return btn
    }

    /// 设置颜色
    override func setTintColor(_ color: UIColor) {
        label.textColor = color
        imageView.layer.borderColor = UIColor.wmc_grayH8_8A8A99.cgColor
    }
    
    /// 进行布局
    override func layoutImgText() {
        imageView.contentMode = .scaleToFill
        imageView.layer.masksToBounds = true
        imageView.layer.cornerRadius = 4
        imageView.layer.borderWidth = 2
        imageView.snp.remakeConstraints { make in
            make.top.equalTo(contentEdge.top)
            make.centerX.equalToSuperview()
            make.size.equalTo(36)
        }
        
        imageView.addSubview(animationImageView)
        animationImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        label.font = 12.yp.uiFont(weight: .medium)
        label.textColor = .black
        label.snp.remakeConstraints { make in
            make.left.equalTo(contentEdge.left)
            make.right.equalTo(-contentEdge.right)
            make.bottom.equalTo(-contentEdge.bottom)
            make.top.equalTo(imageView.snp.bottom).offset(6)
        }
        
        setTintColor(.wmc_grayH8_8A8A99)
    }
    
    func setImage(_ image: UIImage) {
        animationImageView.isHidden = false
        animationImageView.image = image
        animationImageView.transform = CGAffineTransform.YP.CreateScale(0.1)
        Wmc_SpringAnimation { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.animationImageView.transform = CGAffineTransform.YP.CreateScale(1)
        }completion: { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.imageView.image = image
            weakSelf.animationImageView.isHidden = true
        }
    }
}
